package com.example.patientlist;

import com.example.patientlist.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;

public class PatientActivity extends Activity {
	//SwipeGestureListener gestureListener;
	private GestureDetector gesturedetector = null;
	View layout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_patient);
	    setContentView(R.layout.mixed_layout);
	    
	    // do some rearrangement of views with installed action
	    /*
	    View.OnClickListener leftright_listener = this.new LeftRightResizer();
	    final Button right_button = (Button) findViewById(R.id.rightbutton);
	    right_button.setOnClickListener(leftright_listener);
	    final Button left_button = (Button) findViewById(R.id.leftbutton);
	    left_button.setOnClickListener(leftright_listener);
	    */
	    layout = (LinearLayout)findViewById(R.id.gesturing);
	    //layout.setAdapter(arrayAdapter);
	    //gestureListener = new SwipeGestureListener(PatientActivity.this);
	    //layout.setOnTouchListener(gestureListener);
	    gesturedetector = new GestureDetector(new MyGestureListener());

	    layout.setOnTouchListener(new OnTouchListener() {

	    @Override
	    public boolean onTouch(View v, MotionEvent event) {
	    gesturedetector.onTouchEvent(event);
	    return true;
	    }

	    });

	    }

	    public boolean dispatchTouchEvent(MotionEvent ev){

	    super.dispatchTouchEvent(ev);

	    return gesturedetector.onTouchEvent(ev);

	    }

	    class MyGestureListener extends GestureDetector.SimpleOnGestureListener{
	    private static final int SWIPE_MIN_DISTANCE = 150;
	    private static final int SWIPE_MAX_OFF_PATH = 100;
	    private static final int SWIPE_THRESHOLD_VELOCITY = 100;
		Fragment f1,f2,f3;
		View v1,v2,v3; // resize these views according to manipulations
		float weight1, weight2, weight3;
		boolean hid1, hid2, hid3; // nobody hidden
		int everyheight; //assume we all have same height
		private static final float resizeStep = (float) 1.5;
		
		public MyGestureListener() {
			super();
			// TODO Auto-generated constructor stub
			v1 = findViewById(R.id.fragment1);
			v2 = findViewById(R.id.fragment2);
			v3 = findViewById(R.id.fragment3);
			weight1 = ((LinearLayout.LayoutParams) v1.getLayoutParams()).weight;
			weight2 = ((LinearLayout.LayoutParams) v2.getLayoutParams()).weight;
			weight3 = ((LinearLayout.LayoutParams) v3.getLayoutParams()).weight;
			everyheight = ((LinearLayout.LayoutParams) v1.getLayoutParams()).height;
			
			FragmentManager fm = getFragmentManager();
	        f1 = fm.findFragmentById(R.id.fragment1);
	        f2 = fm.findFragmentById(R.id.fragment2);
	        f3 = fm.findFragmentById(R.id.fragment3);
	        hid1 = f1.isHidden();
	        hid2 = f2.isHidden();
	        hid3 = f3.isHidden();		
		}
		

	    @Override
	    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
	    float dX = e2.getX()-e1.getX();
	    float dY = e1.getY()-e2.getY();

	    if (Math.abs(dY)<SWIPE_MAX_OFF_PATH && Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY && Math.abs(dX)>=SWIPE_MIN_DISTANCE ) {

	    	if (dX<0) {
	    		Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
	    		if(hid1) { // hide 1 and 2
					hid1 = hid2 = true;
					hid3 = false;
				}
	    		else if(hid2){ // show 2 if 1 not hidden
					hid2 = false;
				}
	    		else if(hid3){ // show 3 if 2 not hidden
					hid3 = false;
				}
	    		else if(!hid1){ //hide it if no one hidden
					hid1 = true;
				}
	    		else if(!hid2){ // hide it - should not be needed if first if works OK
					hid1 = hid2 = true;
				}
	    		
	    	} 
	    	else {
	    		Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
	    		if(hid3) { // hide 1 and 2
					hid3 = hid2 = true;
					hid1 = false;
				}
	    		else if(hid2){ // show 2 if 3 not hidden
					hid2 = false;
				}
	    		else if(hid1){ // show 1 if 2 not hidden
					hid1 = false;
				}
	    		else if(!hid3){ //hide it if no one hidden
					hid3 = true;
				}
	    		else if(!hid2){ // hide it - should not be needed if first if works OK
					hid3 = hid2 = true;
				}
	    	}
	    
			FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (hid1) ft.hide(f1); else ft.show(f1);
            if (hid2) ft.hide(f2); else ft.show(f2);
            if (hid3) ft.hide(f3); else ft.show(f3);
            ft.commit();
	    return true;
	    } 
	    else if (Math.abs(dX)<SWIPE_MAX_OFF_PATH && Math.abs(velocityY)>=SWIPE_THRESHOLD_VELOCITY && Math.abs(dY)>=SWIPE_MIN_DISTANCE ) {
	    	if (dY>0) {
	    		Toast.makeText(getApplicationContext(), "Up Swipe", Toast.LENGTH_SHORT).show();
	    	} 
	    	else {
	    		Toast.makeText(getApplicationContext(), "Down Swipe", Toast.LENGTH_SHORT).show();
	    	}
	    return true;
	    }
	    return false;
	    }
    }
	
	
	

	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.patient, menu);
		return true;
	}
	
	
	// A handler to resize a vertical linear layout, A better approach is to make a new class of the layout itself.
	/*
	public class LeftRightResizer implements View.OnClickListener {
		Fragment f1,f2,f3;
		View v1,v2,v3; // resize these views according to manipulations
		float weight1, weight2, weight3;
		boolean hid1, hid2, hid3; // nobody hidden
		int everyheight; //assume we all have same height
		private static final float resizeStep = (float) 1.5;
		
		
		
		
		public LeftRightResizer() {
			super();
			// TODO Auto-generated constructor stub
			v1 = findViewById(R.id.fragment1);
			v2 = findViewById(R.id.fragment2);
			v3 = findViewById(R.id.fragment3);
			weight1 = ((LinearLayout.LayoutParams) v1.getLayoutParams()).weight;
			weight2 = ((LinearLayout.LayoutParams) v2.getLayoutParams()).weight;
			weight3 = ((LinearLayout.LayoutParams) v3.getLayoutParams()).weight;
			everyheight = ((LinearLayout.LayoutParams) v1.getLayoutParams()).height;
			
			FragmentManager fm = getFragmentManager();
	        f1 = fm.findFragmentById(R.id.fragment1);
	        f2 = fm.findFragmentById(R.id.fragment2);
	        f3 = fm.findFragmentById(R.id.fragment3);
	        hid1 = f1.isHidden();
	        hid2 = f2.isHidden();
	        hid3 = f3.isHidden();		
		}

		public void onClick(View v){

			// Ok manipulate something

			switch(v.getId()){
			case R.id.rightbutton: // shift weight from 1 to 3
				if(hid1) { // hide 1 and 2
					hid1 = hid2 = true;
					hid3 = false;
					break;
				}
				if(hid2){ // show 2 if 1 not hidden
					hid2 = false;
					break;
				}
				if(hid3){ // show 3 if 2 not hidden
					hid3 = false;
					break;
				}
				if(!hid1){ //hide it if no one hidden
					hid1 = true;
					break;
				}
				if(!hid2){ // hide it - should not be needed if first if works OK
					hid1 = hid2 = true;
				}
				break;
				
			case R.id.leftbutton: // shift weight from 3 to 1
				if(hid3) { // hide 1 and 2
					hid3 = hid2 = true;
					hid1 = false;
					break;
				}
				if(hid2){ // show 2 if 3 not hidden
					hid2 = false;
					break;
				}
				if(hid1){ // show 1 if 2 not hidden
					hid1 = false;
					break;
				}
				if(!hid3){ //hide it if no one hidden
					hid3 = true;
					break;
				}
				if(!hid2){ // hide it - should not be needed if first if works OK
					hid3 = hid2 = true;
				}
				break;
			}

			FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (hid1) ft.hide(f1); else ft.show(f1);
            if (hid2) ft.hide(f2); else ft.show(f2);
            if (hid3) ft.hide(f3); else ft.show(f3);
            ft.commit();

			// Toast.makeText(PatientActivity.this, "Id " + v.getId() + " weight " + 	weight1 + ":" + weight2 +":"+weight3, Toast.LENGTH_SHORT).show();


		}
	}
	*/
	
	// A handler to resize a vertical linear layout, A better approach is to make a new class of the layout itself.
	/*
	public class LeftRightResizerOld implements View.OnClickListener {
		View v1,v2,v3; // resize these views according to manipulations
		float weight1, weight2, weight3;
		int everyheight; //assume we all have same height
		private static final float resizeStep = (float) 1.5;
		
		
		
		
		public LeftRightResizerOld() {
			super();
			// TODO Auto-generated constructor stub
			v1 = findViewById(R.id.fragment1);
			v2 = findViewById(R.id.fragment2);
			v3 = findViewById(R.id.fragment3);
			weight1 = ((LinearLayout.LayoutParams) v1.getLayoutParams()).weight;
			weight2 = ((LinearLayout.LayoutParams) v2.getLayoutParams()).weight;
			weight3 = ((LinearLayout.LayoutParams) v3.getLayoutParams()).weight;
			everyheight = ((LinearLayout.LayoutParams) v1.getLayoutParams()).height;
			
		}
	
		public void onClick(View v){
	
			// Ok manipulate something
	
			switch(v.getId()){
			case R.id.rightbutton: // shift weight from 1 to 3
				weight1 /= resizeStep;
				weight3 *= resizeStep;
	
				
				break;
			case R.id.leftbutton: // shift weight from 3 to 1
				weight1 *= resizeStep;
				weight3 /= resizeStep;
				break;
			}
	
			// Toast.makeText(PatientActivity.this, "Id " + v.getId() + " weight " + 	weight1 + ":" + weight2 +":"+weight3, Toast.LENGTH_SHORT).show();
	
			v1.setLayoutParams(new LinearLayout.LayoutParams(0,everyheight,weight1));
			v2.setLayoutParams(new LinearLayout.LayoutParams(0,everyheight,weight2));
			v3.setLayoutParams(new LinearLayout.LayoutParams(0,everyheight,weight3));
		}
	}
	*/

}
